package com.bbm.communitysystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.core.oss.OSS;
import com.bbm.communitysystem.domain.OssFile;
import com.bbm.communitysystem.domain.SliderImg;
import com.bbm.communitysystem.domain.bo.SliderImgBo;
import com.bbm.communitysystem.domain.dto.SliderImgDto;
import com.bbm.communitysystem.mapper.SliderImgMapper;
import com.bbm.communitysystem.service.ISliderImgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bbm
 * @since 2025-02-13
 */
@Service
@RequiredArgsConstructor
public class SliderImgServiceImpl extends ServiceImpl<SliderImgMapper, SliderImg> implements ISliderImgService {

    private final OSS oss;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SliderImgDto addSliderImg(SliderImgBo sliderImgBo) {

        List<OssFile> ossFiles = oss.saveFile("slider_img", sliderImgBo.getUserId(), sliderImgBo.getImg());

        if (ossFiles.isEmpty()) {
            throw new RuntimeException("上传失败");
        }

        OssFile ossFile = ossFiles.get(0);


        SliderImg sliderImg = new SliderImg();
        sliderImg.setTitle(sliderImgBo.getTitle());
        sliderImg.setHref(sliderImgBo.getHref());
        sliderImg.setImgUrl(ossFile.getClientUrl());
        sliderImg.setPageName(sliderImgBo.getPageName());
        sliderImg.setStatus(1);
        sliderImg.setUpdateTime(System.currentTimeMillis() + "");
        sliderImg.setUpdateBy(sliderImgBo.getUserId());
        sliderImg.setServerPath(ossFile.getServerPath());

        sliderImg.setOssId(ossFile.getId());

        boolean save = save(sliderImg);
        if(!save) {
            throw new RuntimeException("保存失败");
        }

        return BeanUtil.copyProperties(sliderImg, SliderImgDto.class);
    }

    @Override
    public SliderImgDto putSliderImg(SliderImgBo sliderImgBo) {


        List<OssFile> ossFiles = oss.saveFile("slider_img", sliderImgBo.getUserId(), sliderImgBo.getImg());

        if (ossFiles.isEmpty()) {
            throw new RuntimeException("上传失败");
        }

        boolean update = lambdaUpdate().eq(SliderImg::getId, sliderImgBo.getId())
                .set(SliderImg::getUpdateBy, sliderImgBo.getUserId())
                .set(SliderImg::getUpdateTime, System.currentTimeMillis() + "")
                .set(SliderImg::getOssId, ossFiles.get(0).getId())
                .set(SliderImg::getImgUrl, ossFiles.get(0).getClientUrl()).update();
        if (!update) {
            throw new RuntimeException("修改失败");
        }

        SliderImgDto sliderImgDto = new SliderImgDto();

        sliderImgDto.setId(sliderImgBo.getId());
        sliderImgDto.setImgUrl(ossFiles.get(0).getClientUrl());

        return sliderImgDto;
    }

    @Override
    public SliderImgDto delSliderImg(SliderImgBo sliderImgBo) {

        SliderImg byId = getById(sliderImgBo.getId());

        oss.deleteFile(byId.getServerPath());

        boolean b = removeById(sliderImgBo.getId());

        if (!b) {
            return null;
        }

        return new SliderImgDto();
    }


}
